//
//  HomeProdItemView.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/13.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeProdViewFrame.h"
#import "HomeProdItemView.h"
#import "ProductItemInfo.h"
#import "Present.h"
#import "ProdPromotion.h"

#define PROD_PRMT_FONT_SIZE [DimensUtils getScaleX:12]

@interface HomeProdItemView()
{
    UIView *_prodPrmtView;//限时抢购的view
    UILabel *_prodPrmtLabel;//限时抢购的label
    UILabel *_prodPrmtPriceLabel;//限时抢购的价格的label
}



/** 商品图 */
@property (nonatomic, weak) UIImageView *prodImage;
/** 标题 */
@property (nonatomic, weak) UILabel *titleLabel;
/** 现价 */
@property (nonatomic, weak) UILabel *nowPriceLabel;
/** 原价 */
@property (nonatomic, weak) UILabel *oldPriceLabel;
/** 折扣 */
@property (nonatomic, weak) UILabel *disCountLabel;

/** 讨论人数/购买人数 */
@property (nonatomic, weak) UILabel *comtCountLabel;

@property (nonatomic, weak) UIButton *giftBtn;

/** 底部分割线 */
@property (nonatomic,weak)UIView *spliteView;


@end



@implementation HomeProdItemView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 1.商品图
        UIImageView *prodImage = [[UIImageView alloc]init];
        prodImage.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:prodImage];
        //设置layer
        CALayer *layer=[prodImage layer];
        //设置边框线的宽
        [layer setBorderWidth:1];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        
        self.prodImage = prodImage;
        
        _prodPrmtLabel = [[UILabel alloc]init];
        [_prodPrmtLabel setFont:[UIFont systemFontOfSize:PROD_PRMT_FONT_SIZE]];
        [_prodPrmtLabel setText:@"限时抢购"];
        [_prodPrmtLabel setTextColor:[UIColor whiteColor]];
        CGSize prmtSize = [_prodPrmtLabel.text sizeWithFont:_prodPrmtLabel.font];
        [_prodPrmtLabel setFrame:CGRectMake(5, 5, prmtSize.width, prmtSize.height)];
        
        _prodPrmtPriceLabel = [[UILabel alloc]init];
        [_prodPrmtPriceLabel setFont:[UIFont systemFontOfSize:PROD_PRMT_FONT_SIZE]];
        [_prodPrmtPriceLabel setTextColor:[UIColor whiteColor]];
        [_prodPrmtPriceLabel setHeight:_prodPrmtPriceLabel.font.lineHeight];
        [_prodPrmtPriceLabel setY:5];
        
        _prodPrmtView = [[UIView alloc]init];
        [_prodPrmtView setHidden:YES];
        [_prodPrmtView setBackgroundColor:JXColorAlpha(248, 53, 142, 150)];
        
        [_prodPrmtView addSubview:_prodPrmtLabel];
        [_prodPrmtView addSubview:_prodPrmtPriceLabel];
        
        [self addSubview:_prodPrmtView];
        
        
        
        
        // 2.商品标题
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = [UIFont systemFontOfSize:JXProdTitleSize];
        titleLabel.textColor = JXProdTitleColor;
        titleLabel.numberOfLines = 2;
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        // 3.现价
        UILabel *nowPriceLabel = [[UILabel alloc] init];
        nowPriceLabel.textColor = JXThemeColor;
        nowPriceLabel.font = [UIFont systemFontOfSize:JXProdNowPriceSize];
        [self addSubview:nowPriceLabel];
        self.nowPriceLabel = nowPriceLabel;
        
        // 4.原价
        UILabel *oldPriceLabel = [[UILabel alloc] init];
        oldPriceLabel.textColor = JXProdDisCountColor;
        oldPriceLabel.font = [UIFont systemFontOfSize:JXProdDisCountSize];
        [self addSubview:oldPriceLabel];
        self.oldPriceLabel = oldPriceLabel;
        
        // 5.折扣
        UILabel *disCountLabel = [[UILabel alloc]init];
        disCountLabel.textColor = JXProdDisCountColor;
        disCountLabel.font = [UIFont systemFontOfSize:JXProdDisCountSize];
        [self addSubview:disCountLabel];
        self.disCountLabel = disCountLabel;
        
        // 6.讨论人数
        UILabel *comtCountLabel = [[UILabel alloc] init];
        comtCountLabel.textColor = JXProdDisCountColor;
        comtCountLabel.font = [UIFont systemFontOfSize:JXProdDisCountSize];
        comtCountLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:comtCountLabel];
        self.comtCountLabel = comtCountLabel;
        
        // 7.赠品
        //添加赠
        UIButton *giftBtn = [[UIButton alloc]init];
        giftBtn.titleLabel.font = [UIFont systemFontOfSize:[DimensUtils getScaleX:12]];
        giftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        giftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [giftBtn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
        [giftBtn setImage:[UIImage imageNamed:@"product_gift"] forState:UIControlStateNormal];
        giftBtn.hidden = true;
        _giftBtn = giftBtn;
        
        [self addSubview:giftBtn];
        
        
        // 分割线
        UIView *spliteView = [[UIView alloc]init];
        spliteView.backgroundColor = JXColor(242, 242, 242);
        self.spliteView = spliteView;
        [self addSubview:spliteView];
    }
    return self;
}

-(void)setHomeProdFrame:(HomeProdViewFrame *)homeProdFrame{
    _homeProdFrame = homeProdFrame;
    self.frame = homeProdFrame.frame;
    ProductItemInfo *product = homeProdFrame.product;
    
    self.prodImage.frame = homeProdFrame.prodImageFrame;
    [self.prodImage sd_setImageWithURL:[NSURL URLWithString:product.prodImage]];
    
    self.titleLabel.frame = homeProdFrame.titleFrame;
    self.titleLabel.text = product.prodWholeName;
    
    self.nowPriceLabel.frame = homeProdFrame.nowPriceLabelFrame;
    self.nowPriceLabel.text = product.prodNewPrice;
    
    self.oldPriceLabel.frame = homeProdFrame.oldPriceLabelFrame;
    self.oldPriceLabel.text = product.prodOldPrice;
    
    self.disCountLabel.frame = homeProdFrame.disCountLabelFrame;
    self.disCountLabel.text = product.prodDiscount;
    
    self.comtCountLabel.frame = homeProdFrame.comtCountLabelFrame;
    self.comtCountLabel.text = [NSString stringWithFormat:@"%@",product.prodBuyCount];
    
    if(homeProdFrame.giftFrame.size.width==0){
        self.giftBtn.hidden = YES;
        self.oldPriceLabel.hidden = NO;
        self.disCountLabel.hidden = NO;
    }else{
        Present *present = product.prodPromotion.presentList.firstObject;
        [self.giftBtn setTitle:present.name forState:UIControlStateNormal];

        self.giftBtn.hidden = NO;
        self.giftBtn.frame = homeProdFrame.giftFrame;
        self.oldPriceLabel.hidden = YES;
        self.disCountLabel.hidden = YES;
    }
    
    if(product.prodPromotion!=nil && ![Utils isBlankString:product.prodPromotion.price]){
        //显示限时抢购价
        CGFloat prmtX = self.prodImage.x;
        CGFloat prmtH = _prodPrmtPriceLabel.font.lineHeight+JXPublicInset;
        CGFloat prmtY = CGRectGetMaxY(homeProdFrame.prodImageFrame)-prmtH;
        CGFloat prmtW = self.prodImage.width;
        [_prodPrmtView setFrame:CGRectMake(prmtX, prmtY, prmtW, prmtH)];
        
        //设置限时抢购价格
        [_prodPrmtPriceLabel setText:[NSString stringWithFormat:@"￥%@",product.prodPromotion.price]];
        CGSize prmtPriceSize = [_prodPrmtPriceLabel.text sizeWithFont:_prodPrmtPriceLabel.font];
        [_prodPrmtPriceLabel setX:_prodImage.width-prmtPriceSize.width-5];
        [_prodPrmtPriceLabel setWidth:prmtPriceSize.width];
        
        [_prodPrmtView setHidden:NO];
        
    }else{
        [_prodPrmtView setHidden:YES];
    }
    
    CGFloat spliteViewX = JXPublicInset;
    CGFloat spliteViewy = homeProdFrame.frame.size.height-1;
    CGFloat spliteViewW = JXScreenW-JXPublicInset*2;
    CGFloat spliteViewH = 1;
    self.spliteView.frame = CGRectMake(spliteViewX, spliteViewy, spliteViewW, spliteViewH);
//    self.spliteView.hidden = YES;
}

-(void)hideSpliteLine:(BOOL)hide{
    self.spliteView.hidden = hide;
}


@end
